package com.compass.mobile.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.mobile.callback.OnCustomButtonClick
import com.compass.mobile.databinding.LayoutDeptItemBinding
import com.compass.mobile.databinding.LayoutTextMessageItemBinding
import com.compass.mobile.framework.adapter.BaseBindViewHolder
import com.compass.mobile.framework.adapter.BaseMultiItemAdapter
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.models.AIMessageInfo
import saveAs

/**
 * Description： aiMessage  adapter
 * Date:2024/7/1 14:54
 */
class AIMessageAdapter : BaseMultiItemAdapter<AIMessageInfo>() {

    private lateinit var onCustomButtonClick: OnCustomButtonClick
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomButtonClick = onChatBottomClick
    }

    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        val info = getData()[position]
        return info.msgType
    }


    @SuppressLint("SetTextI18n")
    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: AIMessageInfo?,
        position: Int
    ) {
        if (item == null) return
        // 0 是文字 1是科室
        when (item.msgType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutDeptItemBinding>()
                binding.apply {
                    when (item.type) {
                        0 -> {
                            leftLayout.visible()
                            rightLayout.gone()
                            leftMsg.text = item.departments?.name
                            llGoNext.onClick {
                                onCustomButtonClick.onButtonCallBack(2,item.departments?.id)

                            }

                        }

                        1 -> {
                            rightLayout.visible()
                            leftLayout.gone()
                        }
                    }
                }


            }

            else -> {
                val binding = holder.binding.saveAs<LayoutTextMessageItemBinding>()
                binding.apply {
                    when (item.type) {
                        0 -> {
                            leftLayout.visible()
                            rightLayout.gone()
                            leftMsg.text = item.text
                        }

                        1 -> {
                            rightLayout.visible()
                            leftLayout.gone()
                            rightMsg.text = item.text

                        }
                    }
                }

            }

        }


    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {

            1 -> {
                LayoutDeptItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutTextMessageItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }
}